package controller;

import entity.Cart;
import entity.Commodity;
import service.CommodityLogic;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class CartServlet extends HttpServlet {

    //定义购物车的动作
    private String action;
    //商品业务逻辑类对象
    private CommodityLogic cdlogic=new CommodityLogic();


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        if(request.getParameter("action")!=null){
            this.action=request.getParameter("action");
            //动作是add，就执行添加商品到购物车
            if(action.equals("add")){
                if (addToCart(request,response)){
                    request.getRequestDispatcher("/success.jsp").forward(request,response);
                }else {
                    request.getRequestDispatcher("/failure.jsp").forward(request,response);
                }

            }
            //动作是显示，就执行显示购物车
            if(action.equals("show")){
                //内部跳转方式
                request.getRequestDispatcher("/cart.jsp").forward(request,response);
            }
            //动作是删除，执行删除购物车中商品
            if(action.equals("delete")){
                if (deleteFromCart(request,response)){
                    request.getRequestDispatcher("/cart.jsp").forward(request,response);
                }else {
                    request.getRequestDispatcher("/cart.jsp").forward(request,response);
                }

            }
        }

    }
    //添加商品进购物车
    private boolean addToCart(HttpServletRequest request, HttpServletResponse response){
       //获取客户端点击商品的id及数量
        String id=request.getParameter("id");
        String number=request.getParameter("num");
        Commodity cd=cdlogic.getCommodityById(Integer.parseInt(id));
        //是否是第一次给购物车添加商品，第一次需要给session创建一个新的对象
        if (request.getSession().getAttribute("cart")==null){
            Cart cart=new Cart();
            request.getSession().setAttribute("cart",cart);
        }
        Cart cart=(Cart)request.getSession().getAttribute("cart");
        if (cart.addGoodsToCart(cd,Integer.parseInt(number))){
            return true;
        }else{
            return false;
        }

    }
    //从购物车中删除商品
    private boolean deleteFromCart(HttpServletRequest request, HttpServletResponse response) {
        String id=request.getParameter("id");
        Cart cart=(Cart)request.getSession().getAttribute("cart");
        Commodity cd=cdlogic.getCommodityById(Integer.parseInt(id));
        if (cart.removeGoodsFromCart(cd)){
            return true;
        }else {
            return  false;
        }
    }

    public void init() throws ServletException{

    }



}
